package deustotech.mobility.vc.core;

import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.Vector;

import c2x.japi.net.messages.Message;
import c2x.japi.net.messages.MessageListener;
import c2x.japi.net.messages.mgmt.LocationTableMessage;
import c2x.japi.net.messages.mgmt.LocationTableQuery;
import c2x.japi.net.messages.mgmt.MgmtMessageParser;
import c2x.japi.threads.SendReceiveThread;
import c2x.japi.util.LocationTable;
import deustotech.mobility.vc.display.MenuFrame;

public class ServidorGeoCast extends Thread implements MessageListener{

	private static ServidorGeoCast sGC;
	Vector<InetAddress> inets= null;
	private boolean connected=false;
	private Controller c;
	private SendReceiveThread SRT;
	private Vector<InetAddress> inets2;
	private MenuFrame mF;


	public  static synchronized ServidorGeoCast getInstance() {
		if(sGC == null)
			sGC = new ServidorGeoCast();		
		return sGC;
	}

	public  void run(){
		connected = true;
		c = Controller.getInstance();
		mF = c.getmF();
		try {
			Thread.sleep(1000);
		} catch (InterruptedException e1) {
			e1.printStackTrace();
		}
		inets = new Vector<InetAddress>();
		inets2 = new Vector<InetAddress>();
		/**
		 * Obtener la lista de nodos alcanzables y ofrecerla
		 */
		//		LocationTableQuery m = new LocationTableQuery(c.getGateway().getHostAddress(),1500);
		try {
			SRT = new SendReceiveThread(c.getGateway().getHostAddress(), (short)1700,(short)1500,new MgmtMessageParser());
			SRT.addMessageListener(this);
		} catch (Exception e) {
			e.printStackTrace();
		}

		new Thread(){
			public void run(){
				while(connected){
					LocationTableQuery qltMsg = new LocationTableQuery(
							c.getGateway().getHostAddress(), (short)1500);
					SRT.send(qltMsg.assemble());
					System.out.println("Location Table Query sent");
					try {
						Thread.sleep(2000);
					} catch (InterruptedException e) {
						e.printStackTrace();
					}
				}
			}
		}.start();

	}

	public void messageReceived(Message msg) {

		if(msg instanceof LocationTableMessage){

			//			inets2 = new Vector<InetAddress>();
			//			inets2.setSize(0);
			System.out.println("nuevo mensaje con LocationTable");
			LocationTable lt = ((LocationTableMessage) msg).getTable();
			System.out.println("num: "+lt.getNumberOfEntries());
			for(int i = 0; i<lt.getNumberOfEntries();i++){
				System.out.println("C2XVisualizer.messageReceived: Found entry in LT # " + i
						+ " with ID "+  lt.getEntryByIndex(i).getNodeId().numericToTextFormat()
						+ " \n is loc query pending " + lt.getEntryByIndex(i).isLocQueryPending()
						+ " \n is loc valid " + lt.getEntryByIndex(i).isLocValid()
						+ " \n is neighbor " + lt.getEntryByIndex(i).isNeighbor());
				System.out.println("get IPv4 :"+lt.getEntryByIndex(i).getNodeId().getIPv4InetAddress());
				//				inets2.add(lt.getEntryByIndex(i).getNodeId().getIPv4InetAddress());
				mF.addAddress(lt.getEntryByIndex(i).getNodeId().getIPv4InetAddress());
			}

		}else{
			System.out.println("Mensaje recibido, no locationTable msg");

			//			setNewInets(inets2);
			//			System.out.println("SIZE: "+inets.size());
		}

	}

	public Vector<InetAddress> getInets() {
		return inets;
	}

	public void setNewInets(Vector<InetAddress> inets2){
		inets.clear();

		for(int i = 0; i<inets2.size();i++){
			//			inets.add(inets2.get(i));
			inets.add(i,inets2.get(i));
			System.out.println("BLABLA "+inets.get(i).getCanonicalHostName());
			System.out.println(i);
		}
		if(inets.size()<0){
			System.out.println("is not empty");
			mF.addNodes(inets);
		}
		else
			System.out.println("inets = null");
	}



}
